import {
    request,
    AbstractBookCrawler,
    BasicBook,
    SearchBookResult,
    Book,
    BasicChapter,
    BookCrawlerTest,
    BookSource,
} from "../../common/common_node.js";

class BookCrawler extends AbstractBookCrawler {

    async searchBook(keyword, pageNum) {
        function getBoolList($) {
            const bookArr = [];
            const aListBoxArr = $("#alistbox .title");
            aListBoxArr.each(function (index, elem) {
                let nameAndPath = $(this).find("a");
                bookArr.push(new BasicBook({
                    name: nameAndPath.text(),
                    author: $(elem).find('span').text().replace('作者：', ''),
                    path: nameAndPath.attr("href"),
                }));
            })
            return bookArr;
        }

        const $ = await (await request(`${this.source.searchBaseUrl}/search.html`, {
            method: 'POST',
            body: `searchkey=${keyword}&searchtype=all$s=articlename`
        })).doc();
        const bookArr = getBoolList($);

        return new SearchBookResult({
            pageNum: 1,
            pages: 1,
            keyword: keyword,
            bookList: bookArr
        });
    }

    async bookDetails(path) {
        const $ = await (await request(this.source.bookBaseUrl + path,)).doc();

        return new Book({
            name: $("meta[property='og:novel:book_name']").attr('content'),
            author: $("meta[property='og:novel:author']").attr('content'),
            path: path,
            intro: $("meta[property='og:description']").attr('content'),
            coverUrl: $("meta[property='og:image']").attr('content'),
            type: $("meta[property='og:novel:category']").attr('content'),
        });
    }

    async bookCatalog(path) {
        const $ = await (await request(this.source.bookBaseUrl + path)).doc();
        const chapters = $('#list dl dd a');
        let catalog = [];
        chapters.each(function (i, elem) {
            const item = $(elem);
            catalog.push(new BasicChapter({
                index: i,
                title: item.text(),
                path: item.attr("href")
            }));
        });
        return catalog;
    }

    async chapterContent(path) {
        const $ = await (await request(this.source.bookBaseUrl + path)).doc();
        // let content = $("#content").html();
        let content = '';
        const pTags = $('#content').remove('.bottem').find('p');
        pTags.each(function (index) {
            content += $(this).text();
            // 如果不是最后一个p标签，则添加换行符
            if (index !== pTags.length - 1) {
                content += '\n';
            }
        });

        return content;
    }
}

const source = {
    "id": "free_book_js/11",
    "name": "小说旗",
    "portalUrl": "https://www.xs7.com",
    "searchBaseUrl": "https://www.xs7.com",
    "bookBaseUrl": "https://www.xs7.com",
    "active": true
};

await new BookCrawlerTest(new BookCrawler(BookSource.createTestBookSource(source)), {
    enableSearch: false,
    bookPath: "/shu/37479/"
}).run();

//debugger;

